<?php
 /*
 * 1626cms 模板設置
 * ============================================================================
 * 版權所有: 1626網絡，并保留所有權利。
 * 網站地址: http://www.1626.com；
 * ----------------------------------------------------------------------------
 * 這不是一個自由軟件！您只能在不用于商業目的的前提下對程序代碼進行修改和
 * 使用；不允許對程序代碼以任何形式任何目的的再發布。
 * ============================================================================
*/
define('IN_QISHI', true);
require_once('../data/config.php');
require_once('./include/admin_common.inc.php');
require_once(ADMIN_ROOT_PATH.'include/admin_templates_fun.php');
$act = !empty($_REQUEST['act']) ? trim($_REQUEST['act']) : 'list';
$smarty->assign('act',$act);
//列表
if($act == 'list')
{
$dirs = getsubdirs('../templates');
$list=array();
foreach ($dirs as $k=> $val)
{
$list[$k]['dir']=$val;
$list[$k]['info']=get_templates_info("../templates/".$val."/images/info.txt");
}
$smarty->assign('pageheader',"管理中心 - 模板管理 - 選擇模板");
$smarty->assign('list',$list);
$templates['dir']=substr($_CFG['template_dir'],0,-1);
$templates['info']=get_templates_info("../templates/".$templates['dir']."/images/info.txt");
$smarty->assign('templates',$templates);
$smarty->display('admin_templates_list.htm');
}
//備份
if ($act == 'backup')
{
require_once(ADMIN_ROOT_PATH.'include/admin_phpzip.php');
$tpl = trim($_REQUEST['tpl_name']);
$filename = '../temp/backup_templates/' . $tpl . '_' . date('Ymd') . '.zip';
$zip = new PHPZip;
$done = $zip->zip('../templates/' . $tpl . '/', $filename);
if ($done)
{
header("Location:".$filename."");
}
else
{
showmsg("操作失敗！",0);
}
}
//設置
if ($act == 'set')
{
$templates_info=get_templates_info("../templates/".trim($_REQUEST['tpl_dir'])."/images/info.txt");
$tpl_dir = trim($_REQUEST['tpl_dir'])."/";
!$db->query("UPDATE ".table("config")." SET value='".$tpl_dir."' WHERE name='template_dir'")?showmsg('設置失敗',1):get_cache('config',true);
$link[0]['text'] = "返回模板列表";
$link[0]['href'] ="?act=list";
showmsg('保存成功', 2,$link);
}
//編輯模板
if ($act == 'edit')
{
$tpl_dir = trim($_REQUEST['tpl_dir'])?trim($_REQUEST['tpl_dir']):substr($_CFG['template_dir'],0,-1);
$tpl = '';
 	$dir ='../templates/'.$tpl_dir;
 	if($handle = @opendir($dir)){
 		$i = 0;
 		while(false !== ($file = @readdir($handle)))
		{
 			if(substr($file,-4) == '.htm'  && $file != '.' && $file != '..')
			{
 				$list[$i]['name'] = $file;
 				$list[$i]['modify_time'] = date('Y-m-d H:i:s',filemtime($dir.'/'.$file));
 				$list[$i]['size'] = filesize($dir.'/'.$file);
 				$i++;
			}
		}
		array_multisort($list);
}
else
{
showmsg('讀取模板目錄出錯，請檢查讀寫權限', 0);
}
$smarty->assign('pageheader',"管理中心 - 模板管理 - 編輯模板");
$smarty->assign('list',$list);
$templates['dir']=$tpl_dir;
$templates['info']=get_templates_info("../templates/".$templates['dir']."/images/info.txt");
$smarty->assign('templates',$templates);
$smarty->display('admin_templates_file_list.htm');
}
//編輯模板
if ($act == 'edit_file')
{
	$file = $_GET['tpl_name'];
	$file_dir='../templates/'.$_GET['tpl_dir'].'/'.$file;
	if(!$handle = @fopen($file_dir, 'rb')){
	showmsg('打開目標模板文件失敗', 0);
	}
	$tpl['content'] = fread($handle, filesize($file_dir));
	$tpl['content'] = htmlentities($tpl['content'], ENT_QUOTES, GB2312);
	fclose($handle);
	$tpl['name'] = $file;
	$tpl['dir'] = $_GET['tpl_dir'];
	$smarty->assign('pageheader',"管理中心 - 模板管理 - 編輯模板文件");
	$smarty->assign('tpl',$tpl);
	$smarty->display('admin_templates_file_edit.htm');
}
//保存編輯
if ($act == 'do_edit')
{
$tpl_name = !empty($_POST['tpl_name']) ? trim($_POST['tpl_name']) : '';
$tpl_content = !empty($_POST['tpl_content']) ? deep_stripslashes($_POST['tpl_content']) : '';
 	if(empty($tpl_name)){
showmsg('保存模板文件出錯', 0);
 	}
	$file_dir='../templates/'.$_POST['tpl_dir'].'/'.$tpl_name;
 	if(!$handle = @fopen($file_dir, 'wb')){
	showmsg("打開目標模版文件 $tpl_name 失敗",0);
 	}
 	if(fwrite($handle, $tpl_content) === false){
 		showmsg('寫入目標 $tpl_name 失敗,請檢查讀寫權限',0);
 	}
 	fclose($handle);
	$link[0]['text'] = "繼續編輯此文件";
    $link[0]['href'] =$_SERVER['HTTP_REFERER'];
	$link[1]['text'] = "返回模板文件列表";
    $link[1]['href'] ="?act=edit&tpl_dir=".$_POST['tpl_dir'];
 	showmsg('編輯模板成功',2,$link);
	}
?>
